package com.etoak.common.web.context;

import com.etoak.common.jwt.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;

/**
 * 获取登录用户信息
 */
public class LoginContext {

    public static String getUserId(){
        HttpServletRequest request = getRequest();
        String token = request.getHeader(HttpHeaders.AUTHORIZATION);
        Map<String, Object> claimMap = JwtUtil.parse(token);
        return claimMap.get("id").toString();
    }
    
    public static HttpServletRequest getRequest(){
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        return requestAttributes.getRequest();
    }


}
